package com.tanhua.manage.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.tanhua.manage.service.SoulService;
import com.tanhua.model.db.QQuestion;
import com.tanhua.model.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

@RestController
@RequestMapping("/soul")
public class SoulController {

    @Autowired
    private SoulService soulService;

    // 查询问卷列表
    @GetMapping
    public PageResult findList(@RequestParam(defaultValue = "1") Integer pageNumber,
                               @RequestParam(defaultValue = "5") Integer pageSize){


        PageResult pageResult = soulService.findList(pageNumber,pageSize);

        return pageResult;
    }

    // 查询问题列表
    @GetMapping("/question")
    public PageResult selectQuestions(@RequestParam(defaultValue = "1") Integer pageNumber,
                                      @RequestParam(defaultValue = "2") Integer pageSize,
                                      String level){


        PageResult pageResult = soulService.selectQuestions(pageNumber,pageSize,level);

        return pageResult;
    }

    // 保存问题
    @PostMapping("/question/add")
    public Boolean saveQuestions(@RequestBody QQuestion questions){


        // 调用service保存
        try {
            soulService.saveQuestions(questions);

            return true;
        } catch (Exception e) {
            e.printStackTrace();

            return false;
        }

    }

    // 删除问题
    @DeleteMapping("/question/{questionId}")
    public Boolean deleteQuestions(@PathVariable("questionId") String questionId){


        // 调用service删除
        try {
            soulService.deleteQuestions(Integer.valueOf(questionId));
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // 根据id查询问题
    @GetMapping("/question/select/{questionId}")
    public QQuestion selectQuestionById(@PathVariable("questionId") String questionId){

        // 调用service查询
        QQuestion question = soulService.selectQuestionById(Integer.valueOf(questionId));

        // System.out.println(question);

        return question;
    }

    // 修改问题
    @PostMapping("/question/update")
    public Boolean updateQuestions(@RequestBody QQuestion questions){



        // 调用service保存
        try {
            soulService.updateQuestions(questions);

            return true;
        } catch (Exception e) {
            e.printStackTrace();

            return false;
        }

    }
}
